package com.imooc.client.client.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created on 2020/2/12.
 *
 * @author Grak
 * @since 1.0
 */
@RestController
@Slf4j
public class HelloClientController {

//    @Autowired
//    private ProductClient productClient;
//
//    @GetMapping("/getProductMsg")
//    public String getProductClient(){
//        return productClient.productMsg();
//    }


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/restTemp")
    public String getRestTemp(){
        String response = restTemplate.getForObject("http://CLIENT/hello",String.class);
        log.info("response={}",response);
        return response;
    }


    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/client-loadBalancer")
    public String getLoadBalancer(){
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENT");
        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort() + "/hello");
        String response = restTemplate.getForObject(url,String.class);
        log.info("response={}",response);
        return response;
    }

    @GetMapping("/client-hello")
    public String getHello(){
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:8080/hello",String.class);
        log.info("response={}",response);
        return response;
    }



}
